package com.lemon.ui.codemaker;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.KeyPair;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import org.apache.commons.codec.binary.Base64;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;

public class KeyPairGenerator extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JComboBox<String> comboBox;
	private JSpinner spinner;
	private JTextArea textArea_1;
	private JTextArea textArea;
	private JCheckBox safeURLCheckBox;

	/**
	 * Create the dialog.
	 */
	public KeyPairGenerator() {
		setModal(true);
		setResizable(false);
		setTitle("密钥生成");
		setType(Type.UTILITY);
		setBounds(100, 100, 633, 419);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.PREF_COLSPEC,},
			new RowSpec[] {
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormSpecs.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),}));
		{
			JLabel lblNewLabel = new JLabel("算法");
			contentPanel.add(lblNewLabel, "2, 2, right, default");
		}
		{
			comboBox = new JComboBox<String>();
			comboBox.setModel(new DefaultComboBoxModel(new String[] {"RSA", "DiffieHellman", "DSA", "EC"}));
			contentPanel.add(comboBox, "4, 2, fill, default");
		}
		{
			JLabel lblNewLabel_3 = new JLabel("密钥长度");
			contentPanel.add(lblNewLabel_3, "6, 2");
		}
		{
			spinner = new JSpinner();
			spinner.setEditor(new JSpinner.NumberEditor(spinner, "0"));
			spinner.setValue(1024);
			spinner.setPreferredSize(new Dimension(200, 22));
			contentPanel.add(spinner, "8, 2, fill, fill");
		}
		{
			safeURLCheckBox = new JCheckBox("安全Base64");
			safeURLCheckBox.setSelected(true);
			contentPanel.add(safeURLCheckBox, "4, 4");
		}
		{
			JLabel lblNewLabel_1 = new JLabel("公钥");
			contentPanel.add(lblNewLabel_1, "2, 6");
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			contentPanel.add(scrollPane, "4, 6, 5, 1, fill, fill");
			{
				textArea = new JTextArea();
				textArea.setLineWrap(true);
				textArea.setRows(5);
				scrollPane.setViewportView(textArea);
			}
		}
		{
			JLabel lblNewLabel_2 = new JLabel("私钥");
			contentPanel.add(lblNewLabel_2, "2, 8");
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			contentPanel.add(scrollPane, "4, 8, 5, 1, fill, fill");
			{
				textArea_1 = new JTextArea();
				textArea_1.setRows(10);
				textArea_1.setLineWrap(true);
				scrollPane.setViewportView(textArea_1);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton btnNewButton = new JButton("生成密钥对");
				btnNewButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						java.security.KeyPairGenerator keyPairGenerator;
						try {
							keyPairGenerator = java.security.KeyPairGenerator.getInstance(comboBox.getSelectedItem().toString());
							keyPairGenerator.initialize((Integer)spinner.getValue());
							KeyPair keyPair = keyPairGenerator.generateKeyPair();
							if(safeURLCheckBox.isSelected()) {
								textArea.setText(Base64.encodeBase64URLSafeString(keyPair.getPublic().getEncoded()));
								textArea_1.setText(Base64.encodeBase64URLSafeString(keyPair.getPrivate().getEncoded()));
							} else {
								textArea.setText(Base64.encodeBase64String(keyPair.getPublic().getEncoded()));
								textArea_1.setText(Base64.encodeBase64String(keyPair.getPrivate().getEncoded()));
							}
						} catch (Exception e1) {
//							e1.printStackTrace();
							JOptionPane.showMessageDialog(null, e1.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
						}
					}
				});
				buttonPane.add(btnNewButton);
			}
			{
				JButton okButton = new JButton("确定");
				okButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}

}
